package com.detrum.costomview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.detrum.base.R;

/**
 * Created by abc on 2018/3/13.
 */

public class InfoShow extends RelativeLayout {
    private ImageView image,update;
    private TextView name,info,btn;
    private int img;
    private String sName,sInfo,sBtn;
    private boolean isShow;
    private InfoShowCall infoShowCall;
    public InfoShow(Context context) {
        super(context);
    }

    public InfoShow(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.info_show,this,true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.InfoShow);
        img = typedArray.getResourceId(R.styleable.InfoShow_img,0);
        sName = typedArray.getString(R.styleable.InfoShow_name);
        sInfo = typedArray.getString(R.styleable.InfoShow_info);
        sBtn = typedArray.getString(R.styleable.InfoShow_btn);
        isShow = typedArray.getBoolean(R.styleable.InfoShow_isShow,true);
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        image = findViewById(R.id.info_image);
        name = findViewById(R.id.info_name);
        info = findViewById(R.id.info);
        btn= findViewById(R.id.info_btn);
        update = findViewById(R.id.update);
        if(img!=0){
            image.setImageResource(img);
        }
        if(sName!=null){
            name.setText(sName);
        }
        if(sInfo!=null){
            info.setText(sInfo);
        }
        if(sBtn!=null){
            btn.setText(sBtn);
        }
        if(isShow){
            btn.setVisibility(View.VISIBLE);
        }else{
            btn.setVisibility(View.INVISIBLE);
        }
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(infoShowCall!=null){
                    infoShowCall.onInfoClick(InfoShow.this);
                }
            }
        });
    }

    public void setInfoShowCall(InfoShowCall infoShowCall){
        this.infoShowCall = infoShowCall;
    }

    public void setName(String text){
        if(text!=null&&name!=null){
            name.setText(text);
        }
    }

    public void setInfo(String text){
        if(text!=null&&info!=null){
            info.setText(text);
        }
    }

    public void setBtnPadding(int padding){
        btn.setPadding(padding,padding,padding,padding);
    }

    public void btnShow(boolean show){
        if(show){
            btn.setVisibility(View.VISIBLE);
        }else{
            btn.setVisibility(View.INVISIBLE);
        }
    }

    public void updateShow(boolean show){
        if(show){
            update.setVisibility(View.VISIBLE);
        }else{
            update.setVisibility(View.GONE);
        }
    }

}
